<?php
class Admin_Service_Upload {
	/**
	 * 递归创建路径
	 *
	 * @param
	 *        	$path
	 */
	protected static function mkdirs($path) {
		if (! file_exists ( $path )) {
			self::mkdirs ( dirname ( $path ) );
			mkdir ( $path, 0777 );
		}
	}
	/**
	 * 获取目录
	 *
	 * @param
	 *        	$path
	 */
	public static function getdir($mkdir = false) {
		$now = date ( "Y/m/d/" );
		$path = APPLICATION_PATH . '/../data/upload/' . $now;
		if ($mkdir) {
			self::mkdirs ( $path );
		}
		return array (
				"abPath" => $path,
				'path' => $now 
		);
	}
	/**
	 * 缩略图
	 *
	 * @param $path 图片路径        	
	 * @param $name 图片名称        	
	 * @param $width 图片宽度        	
	 * @param $height 图片高度        	
	 * @param $bgcolor 图片背景颜色
	 *        	@判断长和宽的长度，计算比例
	 */
	public static function mkThumb($res_path, $width = 100, $height = 100, $bgcolor = "FFFFFF") {
		$org_info = @getimagesize ( $res_path );
		
		switch ($org_info [2]) {
			case 1 :
			case 'image/gif' :
				$res = imagecreatefromgif ( $res_path );
				break;
			
			case 2 :
			case 'image/pjpeg' :
			case 'image/jpeg' :
			case 'image/jpg' :
				$res = imagecreatefromjpeg ( $res_path );
				break;
			
			case 3 :
			case 'image/x-png' :
			case 'image/png' :
				$res = imagecreatefrompng ( $res_path );
				break;
			
			default :
				return false;
		}
		
		$img_org = $res;
		
		/*
		 * 原始图片以及缩略图的尺寸比例
		 */
		$scale_org = $org_info [0] / $org_info [1];
		$img_thumb = imagecreatetruecolor ( $width, $height );
		$red = $green = $blue = "";
		sscanf ( $bgcolor, "%2x%2x%2x", $red, $green, $blue );
		$clr = imagecolorallocate ( $img_thumb, $red, $green, $blue );
		imagefilledrectangle ( $img_thumb, 0, 0, $width, $height, $clr );
		if ($org_info [0] / $width > $org_info [1] / $height) {
			$lessen_width = $width;
			$lessen_height = $width / $scale_org;
		} else {
			/*
			 * 原始图片比较高，则以高度为准
			 */
			$lessen_width = $height * $scale_org;
			$lessen_height = $height;
		}
		$dst_x = ($width - $lessen_width) / 2;
		$dst_y = ($height - $lessen_height) / 2;
		@imagecopyresampled ( $img_thumb, $img_org, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $org_info [0], $org_info [1] );
		
		$thumb = "";
		if (function_exists ( 'imagejpeg' )) {
			$thumb .= '.thumb.jpg';
			imagejpeg ( $img_thumb, $res_path . $thumb, 100 );
		} elseif (function_exists ( 'imagegif' )) {
			$thumb .= '.thumb.gif';
			imagegif ( $img_thumb, $res_path . $thumb );
		} elseif (function_exists ( 'imagepng' )) {
			$thumb .= '.thumb.png';
			imagepng ( $img_thumb, $res_path . $thumb );
		}
		imagedestroy ( $img_thumb );
		imagedestroy ( $img_org );
		
		return array (
				"x" => $dst_x,
				"y" => $dst_y,
				"thumb" => $thumb 
		);
	}
	public function orgUpload($baseUrl) {		
		/*
		 * ! upload demo for php @requires xhEditor @author
		 * Yanis.Wang<yanis.wang@gmail.com> @site http://xheditor.com/ @licence
		 * LGPL(http://www.opensource.org/licenses/lgpl-license.php) @Version:
		 * 0.9.6 (build 111027) 注1：本程序仅为演示用，请您务必根据自己需求进行相应修改，或者重开发
		 * 注2：本程序特别针对HTML5上传，加入了特殊处理
		 */
		header ( 'Content-Type: text/html; charset=UTF-8' );
		
		$inputName = 'filedata'; // 表单文件域name
		$attachDir = APPLICATION_PATH.'/../public/upload'; // 上传文件保存路径，结尾不要带/
		$dirType = 1; // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
		$maxAttachSize = 2097152; // 最大上传大小，默认是2M
		$upExt = 'txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid'; // 上传扩展名
		$msgType = 2; // 返回上传参数的格式：1，只返回url，2，返回参数数组
		$immediate = isset ( $_GET ['immediate'] ) ? $_GET ['immediate'] : 0; // 立即上传模式，仅为演示用
		ini_set ( 'date.timezone', 'Asia/Shanghai' ); // 时区
		
		$err = "";
		$msg = "''";
		$tempPath = $attachDir . '/' . date ( "YmdHis" ) . mt_rand ( 10000, 99999 ) . '.tmp';
		$localName = '';
		// print_r($_SERVER);
		if (isset ( $_SERVER ['HTTP_CONTENT_DISPOSITION'] ) && preg_match ( '/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i', $_SERVER ['HTTP_CONTENT_DISPOSITION'], $info )) { // HTML5上传
			file_put_contents ( $tempPath, file_get_contents ( "php://input" ) );
			$localName = urldecode ( $info [2] );
		} else { // 标准表单式上传
			$upfile = @$_FILES [$inputName];
			if (! isset ( $upfile ))
				$err = '文件域的name错误';
			elseif (! empty ( $upfile ['error'] )) {
				switch ($upfile ['error']) {
					case '1' :
						$err = '文件大小超过了php.ini定义的upload_max_filesize值';
						break;
					case '2' :
						$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
						break;
					case '3' :
						$err = '文件上传不完全';
						break;
					case '4' :
						$err = '无文件上传';
						break;
					case '6' :
						$err = '缺少临时文件夹';
						break;
					case '7' :
						$err = '写文件失败';
						break;
					case '8' :
						$err = '上传被其它扩展中断';
						break;
					case '999' :
					default :
						$err = '无有效错误代码';
				}
			} elseif (empty ( $upfile ['tmp_name'] ) || $upfile ['tmp_name'] == 'none')
				$err = '无文件上传';
			else {
				move_uploaded_file ( $upfile ['tmp_name'], $tempPath );
				$localName = $upfile ['name'];
			}
		}
		
		if ($err == '') {
			$fileInfo = pathinfo ( $localName );
			$extension = $fileInfo ['extension'];
			if (preg_match ( '/' . str_replace ( ',', '|', $upExt ) . '/i', $extension )) {
				$bytes = filesize ( $tempPath );
				if ($bytes > $maxAttachSize)
					$err = '请不要上传大小超过' .  $this->formatBytes ( $maxAttachSize ) . '的文件';
				else {
					switch ($dirType) {
						case 1 :
							$attachSubDir = 'day_' . date ( 'ymd' );
							break;
						case 2 :
							$attachSubDir = 'month_' . date ( 'ym' );
							break;
						case 3 :
							$attachSubDir = 'ext_' . $extension;
							break;
					}
					$attachDir = $attachDir . '/' . $attachSubDir;
					if (! is_dir ( $attachDir )) {
						@mkdir ( $attachDir, 0777 );
						@fclose ( fopen ( $attachDir . '/index.htm', 'w' ) );
					}
					PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
					$newFilename = date ( "YmdHis" ) . mt_rand ( 1000, 9999 ) . '.' . $extension;
					$targetPath = $attachDir . '/' . $newFilename;
					
					rename ( $tempPath, $targetPath );
					@chmod ( $targetPath, 0755 );
					$targetPath = $this->jsonString ( $targetPath );
					if ($immediate == '1')
						$targetPath = '' . $targetPath;
					if ($msgType == 1)
						$msg = "'$targetPath'";
					else
						$msg = "{'url':'" . $this->jsonString($baseUrl)."\/upload\/" . $attachSubDir."\/".$newFilename . "','localname':'" .  $this->jsonString ( $localName ) . "','id':'1'}"; // id参数固定不变，仅供演示，实际项目中可以是数据库ID
				}
			} else
				$err = '上传文件扩展名必需为：' . $upExt;
			
			@unlink ( $tempPath );
		}
		
		return "{'err':'" . $this->jsonString ( $err ) . "','msg':" . $msg . "}";
	}
	public function jsonString($str) {
		return preg_replace ( "/([\\\\\/'])/", '\\\$1', $str );
	}
	public function formatBytes($bytes) {
		if ($bytes >= 1073741824) {
			$bytes = round ( $bytes / 1073741824 * 100 ) / 100 . 'GB';
		} elseif ($bytes >= 1048576) {
			$bytes = round ( $bytes / 1048576 * 100 ) / 100 . 'MB';
		} elseif ($bytes >= 1024) {
			$bytes = round ( $bytes / 1024 * 100 ) / 100 . 'KB';
		} else {
			$bytes = $bytes . 'Bytes';
		}
		return $bytes;
	}
}